package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysDocshareDao;
import yur.wedding.modules.sys.entity.*;
import yur.wedding.modules.sys.service.SysDeptService;
import yur.wedding.modules.sys.service.SysDocshareService;
import yur.wedding.modules.sys.service.SysRoleService;
import yur.wedding.modules.sys.service.SysUserService;
import yur.wedding.utils.PageUtils;

import javax.annotation.Resource;


@Service("sysDocshareService")
public class SysDocshareServiceImpl extends ServiceImpl<SysDocshareDao, SysDocshareEntity> implements SysDocshareService {

    @Resource
    SysUserService user;
    @Resource
    SysRoleService role;
    @Resource
    SysDeptService dept;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDocshareEntity> page = this.page(
                new Query<SysDocshareEntity>().getPage(params),
                new QueryWrapper<SysDocshareEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 查询单个文档权限信息
     * @param dbgyId
     * @return
     */
    @Override
    public PageUtils getShareList(Long dbgyId,Map<String, Object> params) {

        IPage<SysDocshareEntity> page = this.page(
                new Query<SysDocshareEntity>().getPage(params),
                new QueryWrapper<SysDocshareEntity>().eq(true, "docid", dbgyId)
        );
        for (SysDocshareEntity entity : page.getRecords()) {


            if (entity.getSharetype() == 1) {
                SysUserEntity byId = user.getById(entity.getUserid());
                entity.setName(byId.getLastName());

            }
            if (entity.getSharetype() == 2) {
                SysDeptEntity byId = dept.getById(entity.getDepartmentid());
                entity.setName(byId.getName());
            }
            if (entity.getSharetype() == 3) {
                SysRoleEntity byId = role.getById(entity.getRoleid());
                entity.setName(byId.getRoleName());
            }
            if (entity.getSharetype() == 4) {
                entity.setName("所有人");
            }


        }


        return new PageUtils(page);
    }

}
